<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->

    <!-- Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <script>
        $(document).ready(function () {
            $("btn1").click(function () {
                showAll();
            });
            $("btn2").click(function () {
                query();
            })
        });

        function showAll() {
            $.ajax({
                url:'/all',
                type:'get',
                dataType:'json',
                success:function (data) {
                    $('#tabletest').find('tr').remove();
                    tr='<td>id</td><td>Username</td><td>Email</td><td>Poat</td><td>Date</td>'
                    $("#tabletest").append('<tr>'+tr+'</tr>')
                    //方法中传入的参数data为后台获取的数据
                    for (i in data){
                        var tr;
                        tr = '<td>'+data[i].id+'</td>'+'<td>'+data[i].username+'</td>'+'<td>'+data[i].post+'</td>'+'<td>'+data[i].email+'</td>'+'<td>'+data[i].date+'</td>';
                        $("#tabletest").append('<tr>'+tr+'</tr>')
                    }
                }
            })
        }

        function query(){
            $.ajax({
                url:'/getId',
                type:'get',
                dataType:'json',
                contentType:'application/json',
                data:{"id":$("#id").val()},
                success:function (data) {
                    $('#tabletest').find('tr').remove();
                    tr='<td>id</td><td>Username</td><td>Email</td><td>Poat</td><td>Date</td>'
                    $("#tabletest").append('<tr>'+tr+'</tr>')
                    //方法中传入的参数data为后台获取的数据
                   var tr;
                    tr = '<td>'+data.id+'</td>'+'<td>'+data.username+'</td>'+'<td>'+data.post+'</td>'+'<td>'+data.email+'</td>'+'<td>'+data.date+'</td>'
                    $("#tabletest").append('<tr>'+tr+'</tr>')
                },
                error:function (XMLHttpRequest,textStatus,errorThrown) {
                    alert("没有查询到数据");
                    $('#tabletest').find('tr').remove();
                }
            })
        }
    </script>
</head>
<body>
<!--栅格布局-->
<div class="row">
    <div class="col-lg-3"></div>
    <div class="col-lg-6" style="font-size: large" >
        <br>
        <button id="btn1" class="btn btn-default" onclick="showAll()">查看所有数据</button>
        <input id="id" type="text" style="width: 120px" placeholder="请输入id">
        <button id="btn2" class="btn btn-primary" onclick="query()">查询</button>

        <table id = "tabletest" class="table table-hover table-responsive">

        </table>
    </div>
</div>
</body>
</html>